/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.server.modules.api.web;

import com.server.common.utils.StringUtils;
import com.server.modules.api.Base.BaseAppController;
import com.server.modules.api.service.ISettlementAPIService;
import com.server.modules.api.utils.Constants;
import com.server.modules.api.vo.BaseQueryVo;
import com.server.modules.api.vo.SettementVO;
import com.server.modules.api.vo.SettlementItemVo;
import com.server.modules.sys.common.StaticDefine;
import com.server.modules.sys.entity.loan.CustomPay;
import com.server.modules.sys.entity.settlement.Settlement;
import com.server.modules.sys.entity.settlement.item.SettlementItem;
import com.server.modules.sys.utils.AmountUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * 结算api Controller
 * @author 丁光彪
 * @version 2016-11-21
 */
@Controller
@RequestMapping(value = "api/settlement")
public class SettlementApiController extends BaseAppController {
	@Resource
	ISettlementAPIService settlementAPIService;
	/**
	 * 获取尾款结算明细
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/getFinalPaymentDetails")
	public BaseQueryVo getFinalPaymentDetails(BaseQueryVo vo,String customPayId) {
		//登陆校验
		try {
		if (!checkLogin(vo)) {
				return returnMsgAPIService.getRet(Constants.errLogin);
			}
		} catch (Exception e) {
			logger.error(e.getMessage());
			return returnMsgAPIService.getRet(Constants.errLogin);
		}
		try {
            String memberId = vo.getMemberId();
            //缺少订单id
            if(StringUtils.isBlank(customPayId)){
                //参数不全
                return  returnMsgAPIService.getRet(Constants.incompleteData);
            }
            CustomPay customPay = customPayService.get(new CustomPay(customPayId));
            //申请单不存在
            if(customPay==null){
                return returnMsgAPIService.getRet(Constants.notFound);
            }
            Settlement s = settlementAPIService.getFinalPaymentDetails(customPay.getId());
            SettementVO data=new SettementVO();
            if(s!=null){
                //只能查询自己的结算详情
                String memberId1 = s.getMemberId();
                if(memberId1.equals(memberId)){
                    List<SettlementItem> items = s.getItems();
                    long total=0;
                    for (SettlementItem item : items) {
                        SettlementItemVo itemVo=new SettlementItemVo();
                        Long amount = item.getAmount();
                        if(StaticDefine.SETTLEMENT_ITEM_TYPE.OUT.equals(item.getType())){
                            item.setAmount(-item.getAmount());
                            total-=amount;
                            item.setAmountStr( AmountUtils.changeF2Y(-amount)+"元");
                        }else{
                            total+=amount;
                            item.setAmountStr( AmountUtils.changeF2Y(amount)+"元");
                        }
                        itemVo.setAmountStr(item.getAmountStr());
                        itemVo.setAmount(amount);
                        itemVo.setName(item.getAccountName());
                        data.getItems().add(itemVo);
                    }
                    data.setOrders(s.getOrders());
                    data.setTotal(AmountUtils.changeF2Y(total));
                }else{
                    return returnMsgAPIService.getRet(Constants.notFound);
                }
            }
            vo.setData(data);
            return vo;
        }catch (Exception e){
            logger.error(e.getMessage());
            return returnMsgAPIService.getRet(Constants.unKnowError);
        }


	}



}